<?php

class IndexAction extends Action
{
    public function samefriendList()
    {
        $samefriend = D('Friend')->getSameFriend($this->mid, $_GET['id']);
        $this->assign('samefriend', $samefriend);
        $this->display();
    }

    public function find_friend()
    {

        $map['name'] = array('like', '%' . $_GET['name'] . '%');
        $res = D('friend_info')->where($map)->select();

        foreach ($res as $k => $v) {
            if ($v['uid'] != $this->mid) {
                if ($_GET['type'] == 1) {
                    $map1['uid'] = $v['uid'];
                    if ($_GET['type_v'] != null) {
                        $map1['school_name'] = $_GET['type_v'];
                    }

                    $g = D('friend_school')->where($map1)->select();
                    if ($g != null) {
                        $user[$k] = $v;
                        $user[$k]['school'] = $g;
                        $map4['uid'] = $v['uid'];
                        $j = D('friend_work')->where($map4)->select();
                        $user[$k]['company'] = $j;
                        $temp = D('User')->getUserInfo($v['uid']);
                        $user[$k]['head'] = $temp['avatar_middle'];
                        $user[$k]['city'] = D('Friend')->getCityNamebyid($v['now_province']) . D('Friend')->getCityNamebyid($v['now_city']);
                        $user[$k]['samefriend'] = D('Friend')->getSameFriend($this->mid, $v['uid']);
                        $user[$k]['same_count'] = count($user[$k]['samefriend']);
                        $condition['fid'] = $v['uid'];
                        $condition['uid'] = $this->mid;
                        $a = D('friend')->where($condition)->select();
                        if ($a == null) {
                            $user[$k]['isfriend'] = 0;
                        } else {
                            $user[$k]['isfriend'] = 1;
                        }
                    }
                }
                if ($_GET['type'] == 0) {
                    $map2['uid'] = $v['uid'];
                    if ($_GET['type_v'] != null) {
                        $map2['company'] = $_GET['type_v'];
                    }
                    $f = D('friend_work')->where($map2)->select();
                    if ($f != null) {
                        $user[$k] = $v;
                        $map3['uid'] = $v['uid'];
                        $h = D('friend_school')->where($map3)->select();
                        $user[$k]['school'] = $h;
                        $user[$k]['company'] = $f;
                        $temp = D('User')->getUserInfo($v['uid']);
                        $user[$k]['head'] = $temp['avatar_middle'];
                        $user[$k]['city'] = D('Friend')->getCityNamebyid($v['now_province']) . D('Friend')->getCityNamebyid($v['now_city']);

                        $condition['fid'] = $v['uid'];
                        $condition['uid'] = $this->mid;
                        $a = D('friend')->where($condition)->select();
                        if ($a == null) {
                            $user[$k]['isfriend'] = 0;
                        } else {
                            $user[$k]['isfriend'] = 1;
                        }
                    }
                }
            }
        }
        $user1 = getPage($user, 2);
        $this->assign('user1', $user1);
        $this->display();
    }

    public function connection()
    {
        $condition['uid'] = $this->mid;
        $friend = D('friend')->where($condition)->select();
        $this->assign('one_count', count($friend));
        $this->display();
    }

    public function _one_connection()
    {
        $user = D('Friend')->get_one_connection($this->mid);
        $count = D('Friend')->get_two_connection($this->mid);
        $user1 = getPage($user, 5);
        $this->assign('two_count', count($count));
        $this->assign('user1', $user1);
        $this->display();

    }

    public function _two_connection()
    {
        $user = D('Friend')->get_two_connection($this->mid);
        $count = D('Friend')->get_one_connection($this->mid);
        $this->assign('one_count', count($count));
        $user1 = getPage($user, 1);
        $this->assign('user1', $user1);


        $this->display();

    }


    public function index()
    {

        $tip['uid'] = $this->mid;
        $myinfo = D('friend_info')->where($tip)->select();
        $school = D('friend_school')->where($tip)->select();
        foreach ($school as $k => $v) {
            if ($v['type'] == 1) {
                $myschool['college'] = $v['school_name'];
            }
            if ($v['type'] == 5) {
                $myschool['high'] = $v['school_name'];
            }
            if ($v['type'] == 7) {
                $myschool['middle'] = $v['school_name'];
            }
            if ($v['education'] == 'primary') {
                $myschool['primary'] = $v['school_name'];
            }
        }
        $mywork = D('friend_work')->where($tip)->select();
        $this->assign('myinfo', $myinfo);
        $this->assign('myschool', $myschool);
        $this->assign('mywork', $mywork);


        $condition['uid'] = $this->mid;
        $a = D('friend ')->where($condition)->select();
        $new1 = array();
        foreach ($a as $k => $v) {
            $condition1['fid'] = $v['fid'];
            $condition1['uid'] = array('neq', $this->mid);
            $b[$k] = D('friend')->where($condition1)->select();
            if ($b[$k] != null) {
                $new1 = array_merge($new1, $b[$k]);
            }
        }
        foreach ($new1 as $k => $v) {
            $new[$k] = $v['uid'];
        }
        $temp2 = array_count_values($new);

        foreach ($temp2 as $k => $v) {
            $x = D('Friend')->checkisfriend($a, $k);

            if ($v >= 2 && $x == true) {
                $friend[$k] = D('User')->getUserInfo($k);
                $map1['uid'] = $k;
                $temp = D('friend_info')->where($map1)->select();
                $friend[$k]['name'] = $temp['0']['name'];
                $friend[$k]['count'] = $v;
                $map['uid'] = $k;
                $friend[$k]['school'] = D('friend_school')->where($map)->select();
                $z = D('friend_info')->where($map)->select();
                $friend[$k]['now_city'] = D('Friend')->getCityNamebyid($z['0']['now_province']) . D('Friend')->getCityNamebyid($z['0']['now_city']);
            }

        }
        $res = shuffle($friend);


        $this->assign('user1', $friend);
        $this->display();
    }

    public function friend_management()
    {
        $this->display();

    }

    public function friend_applylist()
    {
        $condition['receive_uid'] = $this->mid;
        $apply = D('friend_apply')->where($condition)->select();
        foreach ($apply as $key => $v) {
            $user[$key] = D('User')->getUserInfo($v['apply_uid']);

        }
        $this->assign('user', $user);

        $this->display();

    }


    public function _friendlist()
    {

        $user = D('Friend')->getFriendListbyGroupid($_GET['groupid'], $this->mid);
        $user1 = getPage($user,2);
        $condition['uid'] = $this->mid;
        $group = D('friend_group')->where($condition)->select();
        $this->assign('group', $group);
        $this->assign('user1', $user1);
        $this->assign('groupname', D('Friend')->getGroupNamebyGroupid($_GET['groupid']));
        $this->assign('groupid', $_GET['groupid']);

        $this->display();
    }

    public function _Cfriendlist()
    {

        $condition2['uid'] = $this->mid;
        $group = D('friend_group')->where($condition2)->select();
        $this->assign('group', $group);
        $this->assign('school_name', $_GET['school_name']);

        $school_name = $_GET['school_name'];
        $condition['school_name'] = $school_name;
        $a = D('friend_school')->where($condition)->select();
        foreach ($a as $k => $v) {
            $condition1['uid'] = $this->mid;
            $condition1['fid'] = $v['uid'];
            $b = D('friend')->where($condition1)->select();
            if ($b != null) {

                $user[$k] = D('User')->getUserInfo($v['uid']);
                $condition2['uid'] = $v['uid'];
                $user[$k]['friend_info'] = D('friend_info')->where($condition2)->select();
                foreach ($b as $key => $val) {
                    $user[$k]['group_name'] = D('Friend')->getGroupNamebyGroupid($val['groupid']);
                    $user[$k]['group_id'] = $val['groupid'];
                }
            }

        }
if($_GET['city_id'] != 0){
        $condition3['now_city'] = $condition4['home_city'] = $_GET['city_id'];

        $c = D('friend_info')->where($condition3)->select();
        $d = D('friend_info')->where($condition4)->select();
        $new1 = array_merge($c, $d);

        foreach ($new1 as $k => $v) {
            $condition1['uid'] = $this->mid;
            $condition1['fid'] = $v['uid'];
            $b = D('friend')->where($condition1)->select();
            if ($b != null) {

                $user[$k] = D('User')->getUserInfo($v['uid']);
                $condition2['uid'] = $v['uid'];
                $user[$k]['friend_info'] = D('friend_info')->where($condition2)->select();
                foreach ($b as $key => $val) {
                    $user[$k]['group_name'] = D('Friend')->getGroupNamebyGroupid($val['groupid']);
                    $user[$k]['group_id'] = $val['groupid'];
                }
            }
        }
        }

        $this->assign('user', $user);
        $city_id = $_GET['city_id'];
        $city_name = D('Friend')->getCityNamebyid($city_id);
        $this->assign('city_id', $city_id);
        $this->assign('city_name', $city_name);
        $this->display();
    }

    public function _grouplist()
    {

        $condition['uid'] = $this->mid;
        $group = D('friend_group')->where($condition)->select();
        $user_all_count = D('Friend')->getcount('friend', $condition);
        $condition_wei['uid'] = $this->mid;
        $condition_wei['groupid'] = 0;
        $user_wei_count = D('Friend')->getcount('friend', $condition_wei);
        foreach ($group as $key => $val) {
            $friend[$key]['groupid'] = $val['id'];
            $friend[$key]['groupname'] = $val['groupname'];
            $condition['groupid'] = $val['id'];
            $friend[$key]['count'] = D('Friend')->getcount('friend', $condition);
        }
        /*     dump($friend);
             dump(  $friend[$key]['count'] );*/
        $this->assign('user_all_count', $user_all_count);
        $this->assign('user_wei_count', $user_wei_count);
        $this->assign('friend', $friend);

        $this->display();
    }

    public function addgroup()
    {
        if ($_POST['groupname'] != null) {
            $groupname = $_POST['groupname'];
            $group = D('friend_group');
            $data['groupname'] = $groupname;
            $data['uid'] = $this->mid;
            $group->data($data)->add();
            $this->ajaxReturn('');
        }
    }

    public function delgroup()
    {
        $condition['uid'] = $this->mid;
        $condition['groupid'] = $_POST['groupid'];
        $userlist = D('friend')->where($condition)->order('fname asc')->select();
        foreach ($userlist as $key => $v) {
            $map['id'] = $v['id'];
            $update = D('friend');

            $data['groupid'] = 0;
            $update->where($map)->save($data);
        }
        $group = D('friend_group');
        $condition1['id'] = $_POST['groupid'];
        $group->where($condition1)->delete();
        $this->ajaxReturn('');

    }

    public function changegroup()
    {
        $condition['uid'] = $this->mid;
        $condition['fid'] = $_POST['fid'];
        $friend = D('friend');
        $data['groupid'] = $_POST['groupid'];
        $friend->where($condition)->save($data);
        $return['groupname'] = D('Friend')->getGroupNamebyGroupid($_POST['groupid']);
        $return['fid'] = $_POST['fid'];
        $this->ajaxReturn($return);
    }

    public function editgroup()
    {
        if ($_POST['groupname'] != null) {
            $groupname = $_POST['groupname'];
            $condition['id'] = $_POST['groupid'];
            $group = D('friend_group');
            $data['groupname'] = $groupname;
            $group->where($condition)->save($data);
            $this->ajaxReturn('');
        }
    }

    public function delfriend()
    {
        $friend = D('friend');
        $condition['uid'] = $this->mid;
        $condition['fid'] = $_POST['fid'];
        $friend->where($condition)->delete();
        $this->ajaxReturn('');
    }

    public function a()
    {
        $user = D('user')->select();
        $this->assign('user', $user);
        $this->display();
    }

    public function friend_apply()
    {
        $condition['apply_uid'] = $this->mid;
        $condition['receive_uid'] = $_POST['fid'];
        $check = D('friend_apply')->where($condition)->select();

        $condition1['uid'] = $this->mid;
        $condition1['fid'] = $_POST['fid'];
        $check1 = D('friend')->where($condition1)->select();
        if ($this->mid == $_POST['fid']) {
            $this->ajaxReturn('1', '');
        }

        if ($check != null || $check1 != null) {
            $this->ajaxReturn('', '', 0);
        } else {
            $apply = D('friend_apply');
            $data['apply_uid'] = $this->mid;
            $data['receive_uid'] = $_POST['fid'];
            $apply->data($data)->add();
            sendInfo($this->mid, 'friend_apply', $_POST['fid']);
            $this->ajaxReturn('');
        }
    }

    public function friend_accept()
    {
        $apply = D('friend_apply');
        $condition['apply_uid'] = $_POST['fid'];
        $condition['receive_uid'] = $this->mid;
        $apply->where($condition)->delete();
        D('Friend')->addfriend($this->mid, $_POST['fid']);
        D('Friend')->addfriend($_POST['fid'], $this->mid);
        sendInfo($this->mid, 'friend_accept', $_POST['fid']);
        $this->ajaxReturn('');
    }

    public function friend_refuse()
    {
        $apply = D('friend_apply');
        $condition['apply_uid'] = $_POST['fid'];
        $condition['receive_uid'] = $this->mid;
        $apply->where($condition)->delete();

        sendInfo($this->mid, 'friend_refuse', $_POST['fid']);
        $this->ajaxReturn('');
    }


    public function _distribution()
    {
        $condition['uid'] = $this->mid;
        $temp = D('friend')->where($condition)->select();
        $new = array();
        foreach ($temp as $k1 => $v1) {
            $res[$k1] = D('Friend')->getschoollist($v1['fid']);
            if ($res[$k1] != null) {
                $new = array_merge($new, $res[$k1]);
            }
        }
        $temp1 = array_count_values($new);
        /*  dump($new);*/
        arsort($temp1);
        foreach ($temp1 as $key => $val) {
            $school[$key]['school_name'] = $key;
            $school[$key]['count'] = $val;
        }
        $this->assign('school', $school);
        $new1 = array();
        foreach ($temp as $k1 => $v1) {
            $res1[$k1] = D('Friend')->getcitylist($v1['fid']);
            if ($res1[$k1]['0'] != null || $res1[$k1]['1'] != null) {
                $new1 = array_merge($new1, $res1[$k1]);
            }
        }
        $temp2 = array_count_values($new1);
        arsort($temp2);
        foreach ($temp2 as $key => $val) {
            $city[$key]['city_id'] = $key;
            $city[$key]['city_name'] = D('Friend')->getCityNamebyid($key);
            $city[$key]['count'] = $val;
        }

        $this->assign('city', $city);
        $this->assign('school', $school);

        $this->display();

    }


    public function possible_know()
    {
        $condition['uid'] = $this->mid;
        $a = D('friend ')->where($condition)->select();
        $new1 = array();
        foreach ($a as $k => $v) {
            $condition1['fid'] = $v['fid'];
            $condition1['uid'] = array('neq', $this->mid);
            $b[$k] = D('friend')->where($condition1)->select();
            if ($b[$k] != null) {
                $new1 = array_merge($new1, $b[$k]);
            }
        }
        foreach ($new1 as $k => $v) {
            $new[$k] = $v['uid'];
        }
        $temp2 = array_count_values($new);

        foreach ($temp2 as $k => $v) {
            $x = D('Friend')->checkisfriend($a, $k);

            if ($v >= 2 && $x == true) {
                $friend[$k] = D('User')->getUserInfo($k);
                $map1['uid'] = $k;
                $temp = D('friend_info')->where($map1)->select();
                $friend[$k]['name'] = $temp['0']['name'];
                $friend[$k]['count'] = $v;
                $map['uid'] = $k;
                $friend[$k]['school'] = D('friend_school')->where($map)->select();
                $z = D('friend_info')->where($map)->select();
                $friend[$k]['now_city'] = D('Friend')->getCityNamebyid($z['0']['now_province']) . D('Friend')->getCityNamebyid($z['0']['now_city']);
            }

        }

        $user1 = getPage($friend, 1);
        $this->assign('user1', $user1);
        $this->display();
        /*    dump( $friend);
            dump( $x);*/
    }

}